package dreampharos.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.dbutils.DbUtils;

import java.sql.*;
/**
 * 
 * @author sh_dev
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
 
public class DBUtil {
	
	Connection dbconn; 
    String dburl;
    String dbuser;
    String dbpass;
    
    public DBUtil()
    {
        dbconn = null;
        //dburl = "jdbc:oracle:thin:@192.168.0.100:1521:rock";
        dburl = "jdbc:oracle:thin:@210.109.111.244:1521:dreampharos";
        dbuser = "banhong";
        dbpass = "qksghd83";
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
            //System.out.println("forname passed");
        }
        catch(Exception exception)
        {
            System.out.println(exception);
        }
        try
        {
            dbconn = DriverManager.getConnection(dburl, dbuser, dbpass);
            //System.out.println("connecting ");
        }
        catch(SQLException sqlexception)
        {
            System.out.println(sqlexception);
        }
    }

    public Connection getConnection(boolean commit)
    {
    	try
        {
    		dbconn.setAutoCommit(commit);
        }
        catch(SQLException sqlexception)
        {
            //System.out.println(sqlexception);
        }
        return dbconn;
    }
    
    public Connection getConnection()
    {
    	try
        {
    		dbconn.setAutoCommit(true);
        }
        catch(SQLException sqlexception)
        {
            //System.out.println(sqlexception);
        }
        return dbconn;
    }
    
    /**
     * 
     * @param conn
     * @throws SQLException
     */
    public  void closeQuietly (Connection conn) throws SQLException {
        try {
        	DbUtils.closeQuietly(conn);   
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException("Connection fail");
        }        
    }
    
    /**
     * 
     * @param conn
     * @param stmt
     * @param rs
     * @throws SQLException
     */
    public  void closeQuietly (Connection conn , Statement stmt , ResultSet rs) throws SQLException {
        try {        	 
        	DbUtils.closeQuietly(conn ,stmt ,rs );
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException("Connection fail");
        }        
    } 
    
    public  void rollback (Connection conn) throws SQLException {
        try {
        	DbUtils.rollback(conn);   
        } catch (Exception e) {
            e.printStackTrace();
            throw new SQLException("Connection fail");
        }        
    }

}